<template>
  <div class="todo-box">
    <!-- // 待办工单 -->
    <!-- 表单信息 -->
    <Tickets @subSearch="subSearch" :flagStatus="flagStatus" />
    <!-- 表格信息 -->
    <Table :tableData="tableData" @handleChange="handleChange" :todoBtn="false" :page="pageNum" :limit="pageSize"
      :total="total" @orderData="orderData" @searchNewListApi="searchNewListApi" @getoutboundOrderData="getoutboundOrderData"
      :todoEdit="checkPermi(['system:order:edit'])" :contractFlag="true" />
  </div>
</template>
<script setup>
import { ref, reactive } from 'vue'
import Table from '@/components/table/indexs'
import Tickets from '@/components/Tickets'
import { getselectOrderNoApi } from '@/api/tickets/index.js'
import { checkPermi } from "@/utils/permission.js";
import dayjs from 'dayjs'

// 隐藏单据状态
let flagStatus = ref(false)
let total = ref(100)
let pageNum = ref(1)    // 页数
let pageSize = ref(50)  // 条数

let orderDateLte = ref(null)
let orderDateGte = ref(null)
// 存放用户搜索的数据
let searchData = ref(null)
// 表单数据
const tableData = ref([])
// 待办工单接口
let getselectOrderData = async (v = '') => {
  let paramss = {
        pageNum: pageNum.value,
        pageSize: pageSize.value,
    }
    let params = Object.assign(paramss, v)

  let res = await getselectOrderNoApi(params)
  let { code, rows } = res
  if (code === 200) {
    tableData.value = rows
    total.value = res.total
  }
}
// 删除后调用查询最新的顶单
const orderData = () => {
  getselectOrderData()
}

// 分页器
const handleChange = (v) => {
  let { page, limit } = v.page
    let params = {
        ...searchData.value,
        orderDateLte: orderDateLte.value,
        orderDateGte: orderDateGte.value,
    }
    params.orderDate = null
    pageNum.value = page
    pageSize.value = limit

    getselectOrderData(params)
}
const searchNewListApi = () => {
  getselectOrderData(searchData.value)
}
const getoutboundOrderData = () => {
  getselectOrderData()
}
// 查询
const subSearch = (v) => {
  let obj = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
  }
  let paramss = {
    orderDateLte: v.orderDate ? v.orderDate[0] : null,
    orderDateGte: v.orderDate ? dayjs(v.orderDate[1]).subtract(1,'second').format('YYYY-MM-DD HH:mm:ss') : null,
    ...v
  }
  searchData.value = paramss

  paramss.orderDate = null
  pageNum.value = 1
  let params = Object.assign(obj, paramss)
  getselectOrderData(params)
}

onMounted(() => {
  getselectOrderData()
})
</script>
<style scoped lang='scss'></style>
